package com.mall.service.user;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.user.InviteCodeService;
import com.mall.custom.dao.read.InviteCodeReadMapper;
import com.mall.custom.dao.write.InviteCodeWriteMapper;
import com.mall.pojo.InviteCode;
import com.mall.pojo.InviteCodeExample;
import com.mall.pojo.InviteCodeExample.Criteria;






@Service
public class InviteCodeServiceImpl implements InviteCodeService {

	@Autowired
	private InviteCodeReadMapper inviteCodeReadMapper;

	@Autowired
	private InviteCodeWriteMapper inviteCodeWriteMapper;

	@Override
	public InviteCode getInviteCodeByPhone(String phone) {

		InviteCodeExample example = new InviteCodeExample();
		example.setOrderByClause(" id desc ");
		example.setLimit(10);
		example.setStart(0);
		Criteria criteria = example.createCriteria();
		criteria.andPhoneEqualTo(phone);
		criteria.andStatusGreaterThanOrEqualTo(0);
		criteria.andTypeEqualTo(0);
		List<InviteCode> inviteCodeList = inviteCodeReadMapper.selectByExample(example);
		if (inviteCodeList == null || inviteCodeList.isEmpty()) {
			return null;
		}
 
		InviteCode inviteCode = inviteCodeList.get(0);
		if (inviteCode.getEffectiveTime().getTime() > inviteCode.getGmtCreate().getTime()) {
			return inviteCode;
		}

		inviteCode.setStatus(-4);
		inviteCode.setGmtModify(new Date());
		inviteCode.setReason("过期");
		this.deleteInviteCodeByPhone(phone, inviteCode);

		return null;
	}

	@Override
	public void updateInviteCodeByPhone(String phone) {

	}

	@Override
	public void insertInviteCode(InviteCode inviteCode) {
		this.inviteCodeWriteMapper.insertSelective(inviteCode);
	}

	@Override
	public int deleteInviteCodeByPhone(String phone, InviteCode inviteCode) {
		if (inviteCode.getStatus() != -4) {
			inviteCode.setStatus(-4);
		}
		inviteCode.setGmtModify(new Date());
		return this.inviteCodeWriteMapper.updateByPrimaryKey(inviteCode);
	}

	@Override
	public InviteCode getInviteCodeBySessionIdAndType1(String phone) {
		InviteCodeExample example = new InviteCodeExample();
		Criteria criteria = example.createCriteria();
		criteria.andPhoneEqualTo(phone);
		criteria.andStatusGreaterThanOrEqualTo(0);
		criteria.andTypeEqualTo(1);
		List<InviteCode> inviteCodeList = inviteCodeWriteMapper.selectByExample(example);
		if (inviteCodeList == null || inviteCodeList.isEmpty()) {
			return null;
		}

		InviteCode inviteCode = inviteCodeList.get(0);
		return inviteCode;
	}

	@Override
	public void modifyByPic4InviteCode(InviteCode inviteCode) {
		if (inviteCode.getType() != 1) {
			return;
		}
		this.inviteCodeWriteMapper.updateByPrimaryKeySelective(inviteCode);
	}

	@Override
	public InviteCode getInviteCodeByValid(String valid) {

		InviteCodeExample example = new InviteCodeExample();
		example.setOrderByClause(" id desc ");
		example.setLimit(10);
		example.setStart(0);
		Criteria criteria = example.createCriteria();
		criteria.andCodeEqualTo(valid);
		criteria.andStatusEqualTo(0);
		criteria.andTypeEqualTo(1);
		List<InviteCode> inviteCodeList = inviteCodeReadMapper.selectByExample(example);
		if (inviteCodeList == null || inviteCodeList.isEmpty()) {
			return null;
		}
 
		InviteCode inviteCode = inviteCodeList.get(0);
		if (inviteCode.getEffectiveTime().getTime() > inviteCode.getGmtCreate().getTime()) {
			return inviteCode;
		}

		

		return inviteCode;
	}

	@Override
	public void updateInviteCodeByInviteCode(InviteCode inviteCode) {
		inviteCodeWriteMapper.updateByPrimaryKey(inviteCode);
		
	}

//	@Override
//	public InviteCode getInviteCodeByPhoneForYanZhengMa(String phone) {
//		InviteCodeExample example = new InviteCodeExample();
//		Criteria criteria = example.createCriteria();
//		criteria.andPhoneEqualTo(phone);
//		criteria.andStatusGreaterThanOrEqualTo(0);
//		criteria.andTypeEqualTo(1);
//		List<InviteCode> inviteCodeList = inviteCodeReadMapper.selectByExample(example);
//		if (inviteCodeList == null || inviteCodeList.isEmpty()) {
//			return null;
//		}
//		return inviteCodeList.get(0);
//	}

}
